package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.i18n.I18Config;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * 变量ui
 *
 * @author leero
 */
public class HelpForm extends JBPanel implements ISettingPanel {
    private JPanel rootPanel;
    private Dialog dialog;
    private Project project;

    public JPanel getRootPanel() {
        return rootPanel;
    }


    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    @Override
    public void setContext(SettingListenerContext context) {

    }

    public HelpForm() {
        super();
        initComponent();
    }

    public HelpForm(Project project, SettingListenerContext context) {
        super();
        this.project = project;
        setContext(context);
        initComponent();
    }


    private void initComponent() {
        initUi();
        initAction();
    }

    private void initUi() {
        GridLayoutManager rootLayoutManager = new GridLayoutManager(1, 1);
        rootLayoutManager.setHGap(1);
        rootLayoutManager.setVGap(1);
        rootLayoutManager.setMargin(new Insets(0, 0, 0, 0));
        rootPanel = new JBPanel<>();
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(1, 1));

        initPanel();
        initText();


    }

    private void initText() {
    }

    private void initTopSplitPanel() {
        FlowLayout layout = new FlowLayout();
        layout.setVgap(1);
        layout.setHgap(1);
        layout.setAlignment(FlowLayout.LEFT);

        int topHeight = 30;
        Dimension dimension = new Dimension(-1, topHeight);
    }

    private void initTabbedPanel() {
        GridLayoutManager layoutManager = new GridLayoutManager(1, 1);
        layoutManager.setHGap(1);
        layoutManager.setVGap(1);
        layoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager inputLayoutManager = new GridLayoutManager(3, 1);
        inputLayoutManager.setHGap(1);
        inputLayoutManager.setVGap(1);
        inputLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager moduleLayoutManager = new GridLayoutManager(3, 1);
        moduleLayoutManager.setHGap(1);
        moduleLayoutManager.setVGap(1);
        moduleLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager lineLayoutManager = new GridLayoutManager(1, 2);
        lineLayoutManager.setHGap(1);
        lineLayoutManager.setVGap(1);
        lineLayoutManager.setMargin(new Insets(0, 0, 0, 0));
        JScrollPane jbScrollPane = null;
        JTextPane helpPanel = new JTextPane();
        helpPanel.setEditable(false);
        HTMLEditorKit editorKit = new HTMLEditorKit();
        helpPanel.setEditorKit(editorKit);
        helpPanel.setOpaque(true);
        jbScrollPane = new JBScrollPane(helpPanel);
        jbScrollPane.setPreferredSize(new Dimension(-1, 570));
        I18Config config = new I18Config();
        String locale = config.getLocale();
        StringBuffer content = new StringBuffer();
        content.append("<html><body><div>")
                .append("<h2 id=\"allProject\"><a href=\"https://github.com/aloxc/restplushelp/blob/main/readme-zh.md\" title=\"帮助\">帮助</a></h2>")
                .append("<h2 id=\"allProject\"><a href=\"https://github.com/aloxc/restplushelp/blob/main/readme-en.md\" title=\"Help\">Help</a></h2>")
                .append("<h2 id=\"allProject\"><a href=\"https://github.com/aloxc/restplushelp/blob/main/readme-ja.md\" title=\"助けて\">助けて</a></h2>")
                .append("</div></body></html>");
        helpPanel.addHyperlinkListener(e -> {
            JEditorPane pane = (JEditorPane) e.getSource();
            if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
                return;
            }
            URL url = e.getURL();
            try {
                Desktop.getDesktop().browse(url.toURI());
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (URISyntaxException ex) {
                ex.printStackTrace();
            }
        });
        helpPanel.setText(content.toString());
//            String content = FileUtils.readFileToString(new File(HelpForm.class.getResource("/help/help.md").getFile()), "utf-8");
//            String htmlContent = MDTool.markdown2Html(content);
//            helpPanel.setText(htmlContent);
        Dimension dimension = new Dimension(-1, -1);

        rootPanel.add(jbScrollPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_FIXED,
                        dimension, dimension, dimension));
    }

    private void initPanel() {
        rootPanel = new JPanel();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(1, 1);
        gridLayoutManager.setVGap(1);
        gridLayoutManager.setHGap(1);

        rootPanel.setLayout(gridLayoutManager);
        initTopSplitPanel();
        initTabbedPanel();
        this.add(rootPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        null, null, null));
    }

    public Project getProject() {
        return project;
    }


    private void initAction() {
    }

    @Override
    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void setProject(Project project) {
        this.project = project;
    }

}
